﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using System.Web.Profile;
using TenderSampleApp.Models;
using TenderSampleApp.Models.Profile;
using TenderSampleApp.Code;
using TenderSampleApp.Areas.DAK.Models;

namespace TenderSampleApp.Controllers
{
    public class RequestController : Controller
    {
             [Authorize(Roles = "USER")]
        public ActionResult ChoseTypeOfRequest(int id = 0)
        {
            DAL dl = new DAL();
            string code = id != 0 ? dl.GetVATCode(id) : null;
            MembershipUser user = Membership.GetUser();
            ProfileBase profile = ProfileBase.Create(user.UserName, user.IsApproved);
            string typeOfPerson = profile.GetPropertyValue("typeOfPerson").ToString();

            if (typeOfPerson == "Фізична особа")
            {

                return RedirectToAction("GetPersonRequest", new { code = code });
            }
            else
            {
                return RedirectToAction("GetJuristRequest", new { code = code });
            }
        }

       [Authorize(Roles = "USER")]
        public ActionResult AcceptRequest(string requestTitle)
        {
            ViewData["requestTitle"] = requestTitle;
            return View();
        }

        [HttpGet]
        [Authorize(Roles = "USER")]
        public ActionResult GetJuristRequest(string code)
        {
            DAL dl = new DAL();
            JuristRequest jr = new JuristRequest() { VATCode = code };
            if (User.IsInRole("USER"))
            {
                JuridicalUserProfileModel userprofile = new ProfileMethod().GetJuridicalUserProfileMethod();
                jr.JFApartment = userprofile.Apartment;
                jr.JFBuild = userprofile.Build;
                jr.JFCity = userprofile.City;
                jr.JFDistrict = userprofile.District;
                jr.JFFullName = userprofile.Name;
                jr.JFStreet = userprofile.Street;
                jr.JFRegion = userprofile.Region;
                jr.JFPhoneNumber = userprofile.Phone;
                jr.JFCode = userprofile.Code;
                jr.JFPostIndex = userprofile.Index;
                //--ReqNum--
                Auction curauc=dl.GetCurrentAuction();
                jr.RequestNumberAuction = curauc.Number;
                jr.RequestNumberNumber = dl.GetNextAuctionRequestNumberForJurRequest(curauc.ID);
                jr.RequestNumberPPZ = "000";
                jr.RequestNumberRegion = "50";
                jr.RequestNumberRegistrator = "00";
                //----------
            }
           
            return View(jr);

        }

        [HttpPost]
        [Authorize(Roles = "USER")]
        public ActionResult GetJuristRequest(JuristRequest jr)
        {
            if (ModelState.IsValid)
            {
                DAL dl = new DAL();
                int JuristicID = dl.AddJuridicalPerson(jr);
                dl.AddRepresentativeOfJuridicalPerson(jr, JuristicID);
                return RedirectToAction("AcceptRequest", new { requestTitle = "GetJuristRequest" });
            }
            return View(jr);
        }

        [HttpGet]
        [Authorize(Roles = "USER")]
        public ActionResult GetPersonRequest(string code)
        {
            PersonRequest pr = new PersonRequest() { VATCode = code };
            DAL dl = new DAL();
            PhysicalUserProfileModel userprofile = new ProfileMethod().GetPhysicalUserProfileMethod();
            pr.PersonApartment = userprofile.Apartment;
            pr.PersonBirthdayYear = userprofile.BirthdayYear;
            pr.PersonBuild = userprofile.Build;
            pr.PersonCity = userprofile.City;
            pr.PersonDateOfIssueСertificate = userprofile.DateOfIssueСertificate;
            pr.PersonDistrict = userprofile.District;
            pr.PersonIssuedBy = userprofile.CertificateIssueBy;
            pr.PersonLastName = userprofile.Lastname;
            pr.PersonName = userprofile.FirstName;
            pr.PersonSurname = userprofile.Surname;
            pr.PersonStreet = userprofile.Street;
            pr.PersonRegion = userprofile.Region;
            pr.PersonPhoneNumber = userprofile.Phone;
            pr.PersonNumberСertificate = userprofile.NumberCertificate;
            pr.PersonSeriesСertificate = userprofile.CertificateSeries;
            pr.PersonPostIndex = userprofile.Index;
            //--ReqNum--
            Auction curauc = dl.GetCurrentAuction();
            pr.RequestNumberAuction = curauc.Number;
            pr.RequestNumberNumber = dl.GetNextAuctionRequestNumberForPhysRequest(curauc.ID);
            pr.RequestNumberPPZ = "000";
            pr.RequestNumberRegion = "00";
            pr.RequestNumberRegistrator = "01";
            //----------
            return View(pr);
        }

        [HttpPost]
        [Authorize(Roles = "USER")]
        public ActionResult GetPersonRequest(PersonRequest pr)
        {
            if (ModelState.IsValid)
            {
                DAL dl = new DAL();
                int PersonID = dl.AddPhysicalPerson(pr);
                if (!String.IsNullOrEmpty(pr.DocumentOfConfirm))
                    dl.AddRepresentativeOfPhysicalPerson(pr, PersonID);

                return RedirectToAction("AcceptRequest", new { requestTitle = "GetPersonRequest" });
            }
            return View(pr);
        }

    }
}
